package com.book.store.present.api;

import com.book.store.present.service.BookStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("api")
public class UnClassifyApi {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private BookStoreService helloService;

    @RequestMapping({"/hello","/hello1"})
    public String hello2(){
        List<ServiceInstance> instances = discoveryClient.getInstances("service-book-store");
        if (!instances.isEmpty()) {
            String url = instances.get(0).getUri().toString();
            url = url + "/greeting";
            RestTemplate restTemplate = new RestTemplate();
            return restTemplate.getForObject(url, String.class);
        }
        return "找不到服务";
    }

    @RequestMapping(value = "/hello2")
    public String hello() {
        return restTemplate.getForEntity("http://service-book-store/greeting", String.class).getBody();
    }

    @RequestMapping("/hello3")
    public String hello3(){
        return helloService.greeting(null);
    }

}
